package com.base.cn.platform.os.controller.qa.record;

import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.pay.AlipayUtils;
import com.base.cn.platform.os.common.utils.pay.PayService;
import com.base.cn.platform.os.common.utils.pay.WeiXinUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.qa.record.QaGainAttendRecord;
import com.base.cn.platform.os.entity.qa.record.QaRecordBaseEntity;
import com.base.cn.platform.os.entity.qa.record.QaTeacherCashRecord;
import com.base.cn.platform.os.entity.qa.record.QaTeacherCommission;
import com.base.cn.platform.os.service.manage.config.SysConfigService;
import com.base.cn.platform.os.service.qa.record.QaGainAttendRecordBiz;
import com.base.cn.platform.os.service.qa.record.QaTeacherCashRecordBiz;
import com.base.cn.platform.os.service.qa.record.QaTeacherCommissionBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 老师提现记录Controller
 *
 * @author s.li
 * @date 2018/11/27
 */
@RestController
@RequestMapping("/course")
public class QaTeacherCashRecordController extends BaseController {

    @Autowired
    private QaTeacherCashRecordBiz qaTeacherCashRecordBiz;
    @Autowired
    private QaTeacherCommissionBiz qaTeacherCommissionBiz;
    @Autowired
    private QaGainAttendRecordBiz qaGainAttendRecordBiz;
    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 创建老师提现记录
     * @param teacherId 老师ID
     * @param accountType 提现的账号类型
     * @param account 账号
     * @param realName 账号名真实姓名
     * @param createUserId 创建人ID
     * @param recordType 记录类型，1问答分成，2旁听分成
     * @return  Map<String,Object>
     */
    @RequestMapping("/createQaTeacherCashRecord")
    public Map<String,Object> createQaTeacherCashRecord(@RequestParam("teacherId") BigDecimal teacherId,
                                                        @RequestParam("accountType") int accountType,
                                                        @RequestParam("account") String account,
                                                        @RequestParam("realName") String realName,
                                                        @RequestParam("createUserId") BigDecimal createUserId,
                                                        @RequestParam("recordType") int recordType){
        List<? extends QaRecordBaseEntity> dataList = null;
        if(recordType==1){//查询问答收益记录
            QaTeacherCommission commission = new QaTeacherCommission();
            commission.setTeacherId(teacherId);
            commission.setStatus(1);
            dataList = qaTeacherCommissionBiz.queryQaTeacherCommissionList(commission,false,false,false);
        }else if(recordType==2){//查询旁听分成收益记录
            QaGainAttendRecord gainAttendRecord = new QaGainAttendRecord();
            gainAttendRecord.setUserType(2);//老师类型的
            gainAttendRecord.setStatus(1);//未提现的
            gainAttendRecord.setReceiptorId(teacherId);
            dataList = qaGainAttendRecordBiz.queryGainAttendRecordList(gainAttendRecord,false,false);
        }
        if(!ObjectUtils.isNotEmpty(dataList)){
            return ResultUtil.ERROR("没有可以申请提现的收益");
        }
        qaTeacherCashRecordBiz.createQaTeacherCashRecord(dataList,teacherId,accountType,account,realName,createUserId,recordType);
        return ResultUtil.SUCCESS("提现申请成功");
    }

    /**
     * 分成查询老师申请提现记录
     * @param teacherCashRecord 查询对象
     * @param currentPage 页码
     * @param pageSize 查询数量
     * @return PageInfo<QaTeacherCashRecord>
     */
    @RequestMapping("/findQaTeacherCashRecordPage")
    public PageInfo<QaTeacherCashRecord> findQaTeacherCashRecordPage(@RequestBody QaTeacherCashRecord teacherCashRecord,
                                                                     @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage,
                                                                     @RequestParam(value = "pageSize",required = false,defaultValue = "10") int pageSize){
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);
        return qaTeacherCashRecordBiz.findQaTeacherCashRecordPage(teacherCashRecord,page);
    }

    /**
     * 获取提现申请详情
     * @param teacherCashRecord 查询条件
     * @return Map<String,Object>
     */
    @RequestMapping("/getTeacherCashRecordInfo")
    public QaTeacherCashRecord getTeacherCashRecordInfo(@RequestBody QaTeacherCashRecord teacherCashRecord){
        return qaTeacherCashRecordBiz.getTeacherCashRecordInfo(teacherCashRecord);
    }

    /**
     * 查询问答提现详情的数据列表
     * @param params 查询参数
     * @param recordType 提现类型，1问答分成，2旁听分成
     * @param currentPage 页码
     * @param pageSize 查询数量
     * @return PageInfo<? extends QaRecordBaseEntity>
     */
    @RequestMapping("/getTeacherCashRecordInfoListData")
    public PageInfo<? extends QaRecordBaseEntity> getTeacherCashRecordInfoListData(@RequestBody Map<String,Object> params,
                                                                          @RequestParam("recordType") int recordType,
                                                                          @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage,
                                                                          @RequestParam(value = "pageSize",required = false,defaultValue = "10") int pageSize){
        String paramsStr = gson.toJson(params);
        Pagination page = new Pagination();
        page.setPageSize(pageSize);
        page.setCurrentPage(currentPage);
        PageInfo<? extends QaRecordBaseEntity> pageInfo = null;
        if(recordType==1){
            QaTeacherCommission teacherCommission = gson.fromJson(paramsStr,QaTeacherCommission.class);
            pageInfo = qaTeacherCommissionBiz.queryQaTeacherCommissionListPage(page,teacherCommission,true,false,false);
        }else {
            QaGainAttendRecord gainAttendRecord = gson.fromJson(paramsStr,QaGainAttendRecord.class);
            gainAttendRecord.setReceiptorId(gainAttendRecord.getTeacherId());
            gainAttendRecord.setTeacherId(null);
            gainAttendRecord.setUserType(2);
            pageInfo = qaGainAttendRecordBiz.queryGainAttendRecordListPage(page,gainAttendRecord,true,false);
        }
        return pageInfo;
    }

    /**
     * 通过审核
     * @param id 记录ID
     * @return Map<String,Object>
     */
    @RequestMapping("/adoptApply")
    public Map<String,Object> adoptApply(HttpServletRequest request,
                                         @RequestParam("id") BigDecimal id,
                                         @RequestParam("sysUserId") BigDecimal sysUserId) throws Exception{
        QaTeacherCashRecord teacherCashRecord = qaTeacherCashRecordBiz.findById(id);
        if(ObjectUtils.isEmpty(teacherCashRecord)){
            return ResultUtil.ERROR("提现记录不存在");
        }
        if(teacherCashRecord.getStatus().intValue() !=1){
            return ResultUtil.ERROR("提现记录状态错误");
        }
        if(teacherCashRecord.getPrice()<1){
            return ResultUtil.ERROR("提现金额必须大于1元");
        }
        int recordType = teacherCashRecord.getRecordType();
        String desc = "";
        if(recordType==1){
            desc = "问答分成提现";
        }
        if(recordType==2){
            desc = "旁听分成提现";
        }
        PayService payService = null;
        Map<String,String> paramsData = null;
        String account = teacherCashRecord.getAccount();
        Map<String,Object> resultMap = null;
        TreeMap<String,String> configMap;
        if(teacherCashRecord.getPrice().doubleValue() >0D){
            paramsData = new HashMap<>();
            configMap = sysConfigService.getSysConfigContext(ConfigName.PAY);
            if(teacherCashRecord.getAccountType().intValue()==2){//微信
                if(StringUtils.isEmpty(account)){
                    return ResultUtil.ERROR("未绑定微信信息");
                }
                payService = new WeiXinUtils();
                paramsData.put("openId",account);
                paramsData.put("amount",String.valueOf(teacherCashRecord.getPrice()));//转账金额
                paramsData.put("desc",desc);
                paramsData.put("check_name","FORCE_CHECK");
                paramsData.put("re_user_name",teacherCashRecord.getRealName());
            }else if(teacherCashRecord.getAccountType().intValue()==1){//支付宝
                if(StringUtils.isEmpty(account)){
                    return ResultUtil.ERROR("未绑定支付宝账号");
                }
                TreeMap<String,String> infoMap = sysConfigService.getSysConfigContext(ConfigName.INFO);
                payService = new AlipayUtils();
                paramsData.put("account",account);//收款方账户
                paramsData.put("amount",String.valueOf(teacherCashRecord.getPrice()));//转账金额 单位：元
                paramsData.put("payerShowName",infoMap.get("company"));//付款方姓名
                paramsData.put("realName",teacherCashRecord.getRealName()); //收款方真实姓名
                paramsData.put("remark",desc); //转账备注
            }
            resultMap = payService.payTransfer(request,configMap,paramsData);
        }
        if(resultMap==null || ResultUtil.getSuccess(resultMap)){
            teacherCashRecord.setStatus(2);
            teacherCashRecord.setExamineUserId(sysUserId);
            qaTeacherCashRecordBiz.updateById(teacherCashRecord,null);
            return ResultUtil.SUCCESS("审核提现成功");
        }else{
            return resultMap;
        }
    }
}
